package com.tjshop.android.adapter;

import java.util.Arrays;
import java.util.List;

import com.tjshop.android.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.tjshop.android.model.CouponDetail;
import com.tjshop.android.model.ShopInfo;
import com.tjshop.android.util.ImageLoaderUtils;
import com.tjshop.android.view.PinnedHeaderListView;
import com.tjshop.android.view.PinnedHeaderListView.PinnedHeaderAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class FriendsAdapter extends BaseAdapter implements SectionIndexer,
		PinnedHeaderAdapter, OnScrollListener {
	private int mLocationPosition = -1;
	private List<ShopInfo> headDatas;
	private List<CouponDetail> itemDatas;
	private List<Integer> mFriendsPositions;
	private LayoutInflater inflater;
	private int sections;
	private Context mContext;

	public FriendsAdapter(Context context, List<ShopInfo> headDatas,
			List<CouponDetail> itemDatas, int sections,
			List<Integer> friendsPositions) {
		// TODO Auto-generated constructor stub
		inflater = LayoutInflater.from(context);
		this.headDatas = headDatas;
		this.itemDatas = itemDatas;
		this.sections = sections;
		mFriendsPositions = friendsPositions;
		mContext = context;
	}

	@Override
	public int getCount() {
		return itemDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return itemDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		int section = getSectionForPosition(position);
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.inc_activity_main_fragment_nearby_list_item_2,
					null);
		}
		RelativeLayout mHeaderParent = (RelativeLayout) convertView
				.findViewById(R.id.top);
		RelativeLayout content = (RelativeLayout) convertView
				.findViewById(R.id.content);
		content.setVisibility(View.VISIBLE);
		if (getPositionForSection(section) == position) {
			TextView shopName = (TextView) convertView
					.findViewById(R.id.shopName);
			TextView shopArea = (TextView) convertView
					.findViewById(R.id.shopArea);
			TextView shopDistance = (TextView) convertView
					.findViewById(R.id.shopDistance);
			ShopInfo info = headDatas.get(getSectionForPosition(position));
			mHeaderParent.setVisibility(View.VISIBLE);
			shopName.setText(info.getShop_name());
			shopArea.setText(info.getShop_area());
			shopDistance.setText(info.getDistance() / (1000 * 1000) + "KM");

		} else {
			mHeaderParent.setVisibility(View.GONE);
		}
		CouponDetail detail = (CouponDetail) itemDatas.get(position);
		TextView item_title = (TextView) convertView
				.findViewById(R.id.item_title);
		TextView item_ori_price = (TextView) convertView
				.findViewById(R.id.item_ori_price);
		TextView item_real_price = (TextView) convertView
				.findViewById(R.id.item_real_price);
		TextView item_staus = (TextView) convertView
				.findViewById(R.id.item_staus);
		ImageView item_icon = (ImageView) convertView
				.findViewById(R.id.item_icon);
		item_title.setText(detail.getName());
		item_ori_price.setText(detail.getApp_price());
		item_real_price.setText(detail.getVip_price());
		String iconPath = detail.getIcon();
		item_staus.setText(detail.getLike());
		ImageLoaderUtils.getInstance(mContext)
				.displayImage(iconPath, item_icon);

		return convertView;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		if (view instanceof PinnedHeaderListView) {
			((PinnedHeaderListView) view).configureHeaderView(firstVisibleItem);
		}
	}

	@Override
	public int getPinnedHeaderState(int position) {
		int realPosition = position;
		if (realPosition < 0
				|| (mLocationPosition != -1 && mLocationPosition == realPosition)) {
			return PINNED_HEADER_GONE;
		}
		mLocationPosition = -1;
		int section = getSectionForPosition(realPosition);
		int nextSectionPosition = getPositionForSection(section + 1);
		if (nextSectionPosition != -1
				&& realPosition == nextSectionPosition - 1) {
			return PINNED_HEADER_PUSHED_UP;
		} else {
			return PINNED_HEADER_VISIBLE;
		}

	}

	@Override
	public void configurePinnedHeader(View header, int position, int alpha) {
		RelativeLayout mHeaderParent = (RelativeLayout) header
				.findViewById(R.id.top);
		RelativeLayout content = (RelativeLayout) header
				.findViewById(R.id.content);

		mHeaderParent.setVisibility(View.VISIBLE);
		content.setVisibility(View.GONE);
		TextView shopName = (TextView) header.findViewById(R.id.shopName);
		TextView shopArea = (TextView) header.findViewById(R.id.shopArea);
		TextView shopDistance = (TextView) header
				.findViewById(R.id.shopDistance);
		ShopInfo info = headDatas.get(getSectionForPosition(position));
		mHeaderParent.setVisibility(View.VISIBLE);
		shopName.setText(info.getShop_name());
		shopArea.setText(info.getShop_area());
		shopDistance.setText(info.getDistance() / (1000 * 1000) + "KM");

	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int getPositionForSection(int section) {
		if (section < 0 || section >= this.sections) {
			return -1;
		}
		return mFriendsPositions.get(section);
	}

	@Override
	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		if (position < 0 || position >= getCount()) {
			return -1;
		}
		int index = Arrays.binarySearch(mFriendsPositions.toArray(), position);
		return index >= 0 ? index : -index - 2;
	}

}
